<?php
/**
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/6/2
 * Time: 下午3:34
 */

namespace app\common\model;


use think\Db;
use think\Model;

class CourseOrder extends Model
{
    protected $table = 'jy_course_order';
    protected $autoWriteTimestamp = 'datetime';

    // 判断用户是否已购买
    public function checkBuy($user_id,$course_id,$type){
        $time = date('Y-m-d H:i:s',time()-31536000);
        $order = Db::name('course_order')->where(['user_id'=>$user_id,'course_id'=>$course_id,'status'=>1])->where('create_time','> time',$time)->find();
        if($order){
            return 1;
        }else{
            if($type != 5){
                // 非组合课，查看是否有组合课并且是否已购买组合课
                $parent = Db::name('course_group')->where('child_id',$course_id)->column('parent_id');
                if(!empty($parent)){
                    $time = date('Y-m-d H:i:s',time()-31536000);
                    $parents = implode(',',$parent);
                    $where['user_id'] = ['eq',$user_id];
                    $where['status'] = ['eq',1];
                    $where['course_id'] = ['in',$parents];
                    $where['create_time'] = ['> time',$time];
                    $buy = Db::name('course_order')->where($where)->select();
                    if($buy){
                        return 1;
                    }
                }
            }
            return 0;
        }
    }
}